package com.ereport.controller.opportunityController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.DailyActivityDAO;
import com.ereport.dao.OpportunityDAO;
import com.ereport.entity.DailyActivity;
import com.ereport.entity.Executives;
import com.ereport.entity.OppParts;
import com.ereport.entity.Opportunity;

@Controller
@RequestMapping("/opportunity")
public class OpprController {

	@RequestMapping(value="/open.do", method=RequestMethod.GET)
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		//String org=req.getParameter("org");
		model.setViewName("/jsp/newenquiry.jsp");
		model.addObject("exeName", ex.getExeName());
		return model;
	}
	@RequestMapping(value="/save.do",method = RequestMethod.POST)
	public  ModelAndView insert(  HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		int rCount=Integer.parseInt(req.getParameter("rCount"));
		System.out.println("row count "+rCount );
		String dactid=req.getParameter("dactid");
		System.out.println("dact id "+dactid);
		DailyActivity da=null;
		DailyActivityDAO dadao=new DailyActivityDAO();
		int daid=0;
		if(!dactid.equals("")){
			daid=Integer.parseInt(dactid);
			da=dadao.getDailyAct(daid);
		}
		System.out.println("da id "+da);
		String org=req.getParameter("org");
		String div=req.getParameter("divi");
		String user=req.getParameter("user");
		String eqtype=req.getParameter("enqtype");
		System.out.println("enq type "+eqtype);
		String eqNo=req.getParameter("enqNum");
		String techBid=req.getParameter("techbid");
		System.out.println("bid type "+techBid);
		Boolean tchBid=false;
		if(techBid!=null && techBid.length()!=0 && techBid!=""){
			tchBid=true;
		}
		String priceBid=req.getParameter("pricebid");
		Boolean prcBid=false;
		if(priceBid!=null && priceBid.length()!=0 && priceBid!=""){
		prcBid=true;
		}
		String dDate=req.getParameter("dueDate");
		System.out.println("due date "+dDate);
		DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		Date  date = formatter.parse(dDate); 
		
		
		/*String prob=req.getParameter("prob");
		int probability=0;
		if(prob!=null){
			probability=Integer.parseInt(prob);
		}*/
		String projName=req.getParameter("prjname");
				
		String executive=req.getParameter("exeName");
		String quotCurr=req.getParameter("curr");
		String opprName=req.getParameter("opprname");
		
		
		Calendar currentDate = Calendar.getInstance();
		DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
		String dateNow = formater.format(currentDate.getTime());
		Date cdate=formater.parse(dateNow);
		//System.out.println("current date "+cdate.getTime());
		
		String crdate=formatter.format(cdate.getTime());
		OpportunityDAO opDao=new OpportunityDAO();
		//Long id=opDao.maxid(ex.getId(),eqtype);
		StringBuilder ref=new StringBuilder();
		ref=ref.append(org+"/"+div+"/"+user+"/"+eqtype+"/"+ex.getId()+"/"+crdate);
		Opportunity opp=opDao.getOppId(ex.getId(),ref.toString());
		Opportunity oppr=null;
		if(opp!=null){
			/*opDao.deleteParts(opp.getId());
			opDao.deleteOpp(ex.getId(),ref.toString());*/
			try {
				oppr=new Opportunity(opp.getId(),ex,da,org,div,user,eqtype,tchBid,prcBid,new java.sql.Date(date.getTime()),new java.sql.Date(cdate.getTime()),projName,executive,quotCurr,opprName,ref.toString());
			} catch (NullPointerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
		
		
		try {
			oppr=new Opportunity(ex,da,org,div,user,eqtype,tchBid,prcBid,new java.sql.Date(date.getTime()),new java.sql.Date(cdate.getTime()),projName,executive,quotCurr,opprName,ref.toString());
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		Set<OppParts> prtSet=new HashSet<OppParts>();
		for(int i=1;i<=rCount;i++){
			String partNum=req.getParameter("prtno"+i);
			System.out.println("part number "+partNum);
			String qt=req.getParameter("qnty"+i);
			System.out.println("qty "+qt);
			int qty=0;
			if(qt.length()!=0||qt!=null){
				qty=Integer.parseInt(qt);
			}
			String make=req.getParameter("make"+i);
			String desc=req.getParameter("decri"+i);
			String appli=req.getParameter("applic"+i);
			String specf=req.getParameter("spec"+i);
			OppParts pr=null;
			
			try {
				if(partNum!=null&&qt!=null&&make!=null&&desc!=null){
					pr=new OppParts(partNum,qty,make,desc,appli,specf);
					prtSet.add(pr);
				}
			} catch (NullPointerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		oppr.setOpParts(prtSet);
		opDao.insert(oppr);
		model.setViewName("redirect:open.do");
		return model;
		
	}
	@RequestMapping(value="/enqReport.do",method = RequestMethod.GET)
	public  ModelAndView enqReport(  HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Executives ex=(Executives)session.getAttribute("loginUser");
		int exeid=0;
		if(ex!=null){
			exeid=ex.getId();
		}
		String dactid=req.getParameter("dactid");
		System.out.println("dact id "+dactid);
		int daid=0;
		if(!dactid.equals("")){
			daid=Integer.parseInt(dactid);
		}
		String edate=req.getParameter("edate");
		
		Date dat=null;
		
		DateFormat formt=new SimpleDateFormat("yyyy-MM-dd");
		dat=formt.parse(edate);
		System.out.println("date in contr "+dat);
		dat= new java.sql.Date(dat.getTime());
		System.out.println("date in contr "+dat);
		OpportunityDAO opDao=new OpportunityDAO();
		if(req.getParameter("exe")!=null){
			String exid=req.getParameter("exe");
			exeid=Integer.parseInt(exid);
		}
		if(req.getParameter("exeName")!=null){
			String exid=req.getParameter("exeName");
			exeid=Integer.parseInt(exid);
		}
		//getting daily activity report details by id
		DailyActivity dact=opDao.getDAct(exeid,daid);
		//getting enquiry details if available
		Opportunity opp=opDao.getEnqReport(exeid, daid);
		List<OppParts> opparts=null;
		if(opp!=null){
			opparts=opDao.getParts(opp.getId());
		
		model.setViewName("/jsp/enquiryReport.jsp");
		model.addObject("enqReport", opp);
		model.addObject("partsList", opparts);
		}else{
			model.setViewName("/jsp/newenquiry.jsp");
			model.addObject("dact", dact);
		}
		
		return model;
		
	}
	@RequestMapping(value="/preEnqReport.do",method = RequestMethod.GET)
	public  ModelAndView preEnqReport(  HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		
		String dactid=req.getParameter("dactid");
		System.out.println("dact id "+dactid);
		int daid=0;
		if(!dactid.equals("")){
			daid=Integer.parseInt(dactid);
		}
		String edate=req.getParameter("edate");
		
		Date dat=null;
		
		DateFormat formt=new SimpleDateFormat("yyyy-MM-dd");
		dat=formt.parse(edate);
		System.out.println("date in contr "+dat);
		dat= new java.sql.Date(dat.getTime());
		System.out.println("date in contr "+dat);
		OpportunityDAO opDao=new OpportunityDAO();
		
		Opportunity opp=opDao.getPreEnqReport(daid);
		List<OppParts> opparts=null;
		if(opp!=null){
			opparts=opDao.getParts(opp.getId());
			
			model.setViewName("/jsp/preEnquiryReport.jsp");
			model.addObject("enqReport", opp);
			model.addObject("partsList", opparts);
		}
		return model;
		
	}
}
